package com.sztle.admodule.resp;

import android.os.Build;

import com.google.gson.Gson;
import com.sztle.admodule.someutils.AppUtils;
import com.sztle.admodule.control.manager.ControlManager;
import com.sztle.admodule.control.utils.UserDeviceUtils;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.TimeZone;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class RequestUtils {
    public static RequestBody getRequestMapBody(Map map) {
        if (map == null) {
            return null;
        }
        Gson gson = new Gson();
        String s = gson.toJson(map);
        return getRequestStrBody(s);
    }

    public static RequestBody getRequestObjectBody(Object obj, Class<?> cls) {
        if (obj == null) {
            return null;
        }
        Gson gson = new Gson();
        String s = gson.toJson(obj, cls);
        return getRequestStrBody(s);
    }

    public static RequestBody getRequestObjectBody(Object obj) {
        if (obj == null) {
            return null;
        }
        Gson gson = new Gson();
        String s = gson.toJson(obj);
        return getRequestStrBody(s);
    }

    public static RequestBody getRequestStrBody(String str) {
        if (str == null) {
            return null;
        }
        return RequestBody.create(MediaType.parse("application/json; charset=utf-8"), str);
    }

    public static String getToken() {
        StringBuilder sb = new StringBuilder("");
        Random random = new Random();
        for (int index = 0; index < 10; index++) {
            sb.append(random.nextInt(10));
        }
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        long utcTimestamp = calendar.getTimeInMillis() / 1000;
        sb.append("" + utcTimestamp);
        return sb.toString();
    }

    public static long getTimeUtcSec() {
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        long utcTimestamp = calendar.getTimeInMillis() / 1000;
        return utcTimestamp;
    }

    public static Map getBaseQueryMap() {
        HashMap map = new HashMap();
        map.put("token", getToken());
        map.put("versionName", AppUtils.getAppVersionName());
        map.put("packageName", AppUtils.getAppPackageName());
        return map;
    }

    public static Map getSzHeadMap() {
        HashMap map = new HashMap();
        map.put("TK", getToken());
        map.put("DEVID", UserDeviceUtils.getUUId());
        map.put("PKG", AppUtils.getAppPackageName());
        map.put("VER", AppUtils.getAppVersionName());
        map.put("BRAND", Build.BRAND);
        map.put("MODEL", Build.MODEL);
        map.put("OSV", Build.VERSION.RELEASE);
        map.put("SPDID", UserDeviceUtils.getDid());
        return map;
    }

    public static Map getSzLogHeadMap() {
        HashMap map = new HashMap();
        map.put("TK", getToken());
        map.put("DEVID", UserDeviceUtils.getUUId());
        map.put("PKG", AppUtils.getAppPackageName());
        map.put("VER", AppUtils.getAppVersionName());
        map.put("BRAND", Build.BRAND);
        map.put("MODEL", Build.MODEL);
        map.put("OSV", Build.VERSION.RELEASE);
        map.put("OAID", ControlManager.getOriginOaid());
        map.put("ANID", UserDeviceUtils.getAndroidId());
        map.put("SSOAID", ControlManager.getSSOaid());
        map.put("SPDID", UserDeviceUtils.getDid());
        return map;
    }


//    //图片文件上传用 单图
//    public static MultipartBody.Part getImagePart(UploadImageInfo imageInfo) {
//        if (imageInfo == null || TextUtils.isEmpty(imageInfo.absoluteImagePath)) {
//            return null;
//        }
//        File file = new File(imageInfo.absoluteImagePath);
//        if (TextUtils.isEmpty(imageInfo.imageFileName)) {
//            imageInfo.imageFileName = file.getName();
//        }
//
//        RequestBody body = RequestBody.create(MediaType.parse("multipart/form-data"), file);
//
//        // 将RequestBody对象转换成MultipartBody.Part对象
//        MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), body);
//
//        return part;
//    }
//
//    public static RequestBody getImageBody(UploadImageInfo imageInfo) {
//        if (imageInfo == null || TextUtils.isEmpty(imageInfo.absoluteImagePath)) {
//            return null;
//        }
//        File file = new File(imageInfo.absoluteImagePath);
//        if (TextUtils.isEmpty(imageInfo.imageFileName)) {
//            imageInfo.imageFileName = file.getName();
//        }
//
//        RequestBody body = RequestBody.create(MediaType.parse("image/*"), file);
//        return body;
//    }
//
//    //图片文件上传用 多图
//    public static List<MultipartBody.Part> getImageListPart(List<UploadImageInfo> imageInfoList) {
//        List<MultipartBody.Part> partList = new ArrayList<>();
//        if (imageInfoList == null || imageInfoList.isEmpty()) {
//            return partList;
//        }
//        for (UploadImageInfo imageInfo : imageInfoList) {
//            MultipartBody.Part part = getImagePart(imageInfo);
//            if (part != null) {
//                partList.add(part);
//            }
//        }
//        return partList;
//    }
//
//
//
//    //可以用来上传多文件,以body的形式上传
//    public static MultipartBody getMultiImageFileBody(List<UploadImageInfo> imageInfoList) {
//        if (imageInfoList == null || imageInfoList.isEmpty()) {
//            return null;
//        }
//        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
//
//        for (UploadImageInfo imageInfo : imageInfoList) {
//            File file = new File(imageInfo.absoluteImagePath);
//            RequestBody body = RequestBody.create(MediaType.parse("multipart/form-data"), file);
//            builder.addFormDataPart("file", file.getName(), body);
//        }
//        builder.addFormDataPart("modelName","lora1");
//
//        return builder.build();
//    }

}
